{% extends 'base.html' %}
{% load static %}

{% block title %}用户管理 - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>用户管理</h1>
        <a href="{% url 'users:user_create' %}" class="btn btn-primary">
            <i class="bi bi-plus-circle"></i> 添加用户
        </a>
    </div>

    {% if messages %}
    <div class="alert alert-info">
        {% for message in messages %}
        <div>{{ message }}</div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- 搜索和筛选 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="搜索用户名、姓名或邮箱" value="{{ search_query }}">
                </div>
                <div class="col-md-3">
                    <select name="role" class="form-select">
                        <option value="">所有角色</option>
                        {% for value, label in role_choices %}
                        <option value="{{ value }}" {% if role_filter == value %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-outline-primary w-100">
                        <i class="bi bi-search"></i> 搜索
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{% url 'users:user_list' %}" class="btn btn-outline-secondary w-100">
                        <i class="bi bi-arrow-clockwise"></i> 重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- 用户列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">用户列表</h5>
        </div>
        <div class="card-body">
            {% if users %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>姓名</th>
                            <th>邮箱</th>
                            <th>角色</th>
                            <th>状态</th>
                            <th>注册时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>
                                <strong>{{ user.username }}</strong>
                                {% if user.is_staff %}
                                <span class="badge bg-danger ms-1">管理员</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if user.first_name or user.last_name %}
                                {{ user.first_name }} {{ user.last_name }}
                                {% else %}
                                <span class="text-muted">未设置</span>
                                {% endif %}
                            </td>
                            <td>{{ user.email|default:"未设置" }}</td>
                            <td>
                                {% if user.profile %}
                                <span class="badge bg-{% if user.profile.role == 'admin' %}danger{% elif user.profile.role == 'technician' %}info{% else %}secondary{% endif %}">
                                    {{ user.profile.get_role_display }}
                                </span>
                                {% else %}
                                <span class="badge bg-warning">未配置</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if user.is_active %}
                                <span class="badge bg-success">启用</span>
                                {% else %}
                                <span class="badge bg-danger">禁用</span>
                                {% endif %}
                            </td>
                            <td>{{ user.date_joined|date:"Y-m-d" }}</td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'users:user_detail' user.pk %}" class="btn btn-outline-primary">
                                        <i class="bi bi-eye"></i> 查看
                                    </a>
                                    <a href="{% url 'users:user_edit' user.pk %}" class="btn btn-outline-info">
                                        <i class="bi bi-pencil"></i> 编辑
                                    </a>
                                    {% if user != request.user %}
                                    <form method="post" action="{% url 'users:user_toggle_active' user.pk %}" class="d-inline">
                                        {% csrf_token %}
                                        <button type="submit" class="btn btn-outline-{% if user.is_active %}warning{% else %}success{% endif %}">
                                            <i class="bi bi-{% if user.is_active %}x-circle{% else %}check-circle{% endif %}"></i>
                                            {% if user.is_active %}禁用{% else %}启用{% endif %}
                                        </button>
                                    </form>
                                    {% endif %}
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-5">
                <div class="text-muted">
                    <i class="bi bi-people" style="font-size: 3rem;"></i>
                    <h4 class="mt-3">暂无用户</h4>
                    <p>系统中还没有用户数据</p>
                    <a href="{% url 'users:user_create' %}" class="btn btn-primary mt-2">
                        <i class="bi bi-plus-circle"></i> 添加第一个用户
                    </a>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<style>
.table th {
    background-color: #f8f9fa;
    border-top: none;
}
.btn-group .btn {
    border-radius: 0.25rem;
}
.btn-group .btn:not(:last-child) {
    margin-right: 0.25rem;
}
</style>
{% endblock %}
